// Sunghan Cho
// 301140084
// sunghanc@sfu.ca

#include "studentdialog.h"
#include "ui_studentdialog.h"

StudentDialog::StudentDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::StudentDialog)
{
    ui->setupUi(this);
    QObject::connect(ui->CloseButton, SIGNAL(clicked()), this, SLOT(close()));
}

StudentDialog::~StudentDialog()
{
    delete ui;
}

void StudentDialog::populateForm()
{
    ui->lineEdit_name->insert(m_pSelectedStudent->getName());
    ui->lineEdit_prefName->insert(m_pSelectedStudent->getPreferredName());

    QString stdNum = QString::number(m_pSelectedStudent->getStudentNumber());
    ui->lineEdit_student->insert(stdNum);

    ui->lineEdit_email->insert(m_pSelectedStudent->getEmail());
    ui->lineEdit_courses->insert(m_pSelectedStudent->getCoursesTakenNames());
}

void StudentDialog::clearForm()
{
    ui->lineEdit_name->clear();
    ui->lineEdit_prefName->clear();
    ui->lineEdit_student->clear();
    ui->lineEdit_email->clear();
    ui->lineEdit_courses->clear();
}

void StudentDialog::setSelectedStudent(Item *pStudent)
{
    m_pSelectedStudent = dynamic_cast<Student*>(pStudent);

    if(pStudent != NULL){
        clearForm();
        populateForm();
    }
    if(pStudent == NULL){
        clearForm();
    }
    if(isHidden()){
        show();
        exec();
    }
}
